package VehicleCommServer;

import java.io.IOException;
import java.net.*;
import java.util.*;

/**
 * <pre>
 * @Package : VehicleCommServer
 * @File    : VehicleCommServer.java
 * <pre>
 *
 * @Author  : 차재우
 * @Date    : 2013. 7. 23.
 * @Version :
 */
public class VehicleCommServer {
	
	private ConfigManager configManager = null;
	
	// 클라이언트 소켓 관리 해쉬
	private HashMap<Integer, ReceiverThread> clients = null;
	
	// 클라이언트 소켓
	private Socket socket = null;
	
	// 시리얼 송신 대기 큐
	public SerialQueue sendQueue = new SerialQueue();
	
	// 시리얼 수신 대기 큐
	public SerialQueue recvQueue = new SerialQueue();

	/**
	 * <pre>
	 * @Comment : 생성자
	 * <pre>
	 *
	 */
	public VehicleCommServer() {
		clients = new HashMap<Integer, ReceiverThread>();
		Collections.synchronizedMap(clients);
	}

	/**
	 * <pre>
	 * @Comment : main 함수
	 * <pre>
	 *
	 * @Method  : main
	 * @param args
	 */
	public static void main(String[] args) {
		new VehicleCommServer().ServerStart();
	}

	/**
	 * <pre>
	 * @Comment : 서버 시작 함수
	 * <pre>
	 *
	 * @Method  : ServerStart
	 */
	public void ServerStart() {
		ServerSocket serverSocket = null;

		try {
			serverSocket = new ServerSocket(9872);
			System.out.println("**************************** VehicleCommServer start ****************************");

			while (true) {
				socket = serverSocket.accept();
				Thread receiver = new Thread(new ReceiverThread(this));
				Thread sender = new Thread(new SenderThread(this));
				
				System.out.println("[ " + socket.getInetAddress() + " : " + socket.getPort() + " ]" + "Connect.");
				System.out.println("Thread hashCode : " + socket.hashCode());
				System.out.println("=================================================================================\n");

				receiver.start();
				sender.start();
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @return the clients
	 */
	public HashMap<Integer, ReceiverThread> getClients() {
		return clients;
	}

	/**
	 * @return the socket
	 */
	public Socket getSocket() {
		return socket;
	}

	/**
	 * @return the sendQueue
	 */
	public SerialQueue getSendQueue() {
		return sendQueue;
	}

	/**
	 * @return the recvQueue
	 */
	public SerialQueue getRecvQueue() {
		return recvQueue;
	}
}
